package cn.tedu.tmall.admin.content.service.impl;

import cn.tedu.tmall.admin.content.dao.repository.ICategoryRepository;

import cn.tedu.tmall.admin.content.pojo.param.CategoryAddParam;
import cn.tedu.tmall.admin.content.pojo.po.CategoryPO;
import cn.tedu.tmall.admin.content.service.ICategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class CategoryServiceImpl implements ICategoryService {

    @Autowired
    private ICategoryRepository categoryRepository;
    @Override
    public void add(CategoryAddParam categoryAddParam) {
        log.debug("Service增加类别-入参{}",categoryAddParam);
        String name = categoryAddParam.getName();
        Long count = categoryRepository.countCategoryByName(name);
        log.debug("Service增加类别-入参:{},出参{}",categoryAddParam,count);
       if(count == 0) {
           categoryRepository.save(categoryAddParam);
       }else {
           throw new RuntimeException("类别名称已经存在!!");
       }
    }

    @Override
    public CategoryPO getCategoryById(Long categoryId) {
        return categoryRepository.getCategoryById(categoryId);
    }
}
